﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SdmxMl.Common;
using SdmxMl.Helper;
using SdmxMl.Structure;
using SdmxMl.Manager;

namespace SdmxMl.UC
{
    public partial class UcConcept : UcBase
    {
        private ConceptType ct = null;

        private UcTextFormat ucTextFormat = null;
        public UcConcept()
        {
            InitializeComponent();
            customComboBoxName.DropDownControl = ucMlText;

            ucTextFormat = new UcTextFormat();
            customComboBoxTextType.DropDownControl = ucTextFormat;
        }

        public override bool BindFromNode(TreeNode node, bool isFinal)
        {
            base.BindFromNode(node, isFinal);
            ct = null;
            if (node != null)
                ct = node.Tag as ConceptType;
            IsFinalEditControls(isFinal);
            BindToControls();
            IsFinalEditControls(isFinal);

            return Visible;
        }

        private void BindToControls()
        {
            if (ct != null)
            {
                Visible = true;
                textBoxId.Text = ct.Id;
                FillParentInfo(textBoxParent, textBoxParentCodeDescr);
                textBoxUrn.Text = ct.Urn;
                textBoxUri.Text = ct.Uri;
                ucCodelistRef1.BindRef(ct.CoreRepresentation);
                customComboBoxName.Text = ct.Name.ToString();
                ucMlText.ML = ct.Name;
                comboMultiLinesDescr.Text = ct.Description.ToString();
                comboMultiLinesDescr.ML = ct.Description;
                ucTextFormat.TF = ct.TextFormat;
                customComboBoxTextType.Text = ct.TextFormat.TextType.ToString();
                checkBoxIsExtRef.Checked = ct.IsExternalReference;

            }
            else
                Visible = false;
        }

        // Manage Edition possibility
        private void IsFinalEditControls(bool isFinal)
        {
            textBoxId.ReadOnly = isFinal;
            customComboBoxName.IsFinal = isFinal;
            comboMultiLinesDescr.IsFinal = isFinal;
            textBoxUri.ReadOnly = isFinal;
            textBoxUrn.ReadOnly = isFinal;
            customComboBoxTextType.IsFinal = isFinal;
            checkBoxIsExtRef.Enabled = isFinal == false;
            if (ucTextFormat.TF != null)
                ucTextFormat.IsFinal = isFinal;
            ucCodelistRef1.IsFinal = isFinal;
        }

        protected override bool BindFromControls()
        {
            bool err = false;
            if (ct != null)
            {
                err = CheckUniqueId(textBoxId);
                ct.Urn = textBoxUrn.Text;
                ct.Uri = textBoxUri.Text;
                ct.Name.UpdateForCurrentLanguage(customComboBoxName.Text);
                ct.Description.UpdateForCurrentLanguage(comboMultiLinesDescr.Text);
                ct.TextFormat = ucTextFormat.TF;
                //customComboBoxTextType.Text = ct.TextFormat.TextType.ToString();
            }
            return err;
        }

        private void customComboBoxTextType_DropDownClosed(object sender, EventArgs e)
        {
            customComboBoxTextType.Text = ct.TextFormat.TextType.ToString();
    //        if (ucTextFormat.TF != null)
      //          customComboBoxTextType.Text = ucTextFormat.TF.TextType.ToString();
        }

        private void buttonCopy_Click(object sender, EventArgs e)
        {
            if (ucTextFormat.TF != null)
            {

                CopyPasteManager.TextFormat = new TextFormatType(ucTextFormat.TF);
            }
            
        }

        private void buttonPaste_Click(object sender, EventArgs e)
        {
            if (ucTextFormat.TF != null && CopyPasteManager.TextFormat != null && ucTextFormat.IsFinal == false)
            {
                ucTextFormat.TF = new TextFormatType(CopyPasteManager.TextFormat);
                customComboBoxTextType.Text = ucTextFormat.TF.TextType.ToString();
            }

        }

    }
}
